VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "LPedido"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
'---------------------------------------------------------------------------------------
' Module    : LPedido
' DateTime  : 30/11/2011 12:18
' Author    :
' Purpose   : Clase Logica Negocio Pedido
'---------------------------------------------------------------------------------------
Option Explicit
Dim dao As DAOPedido
Dim logDAO As DAOLog
Dim MesaDAO As DAOMEsa
Private Const MODULO As String = "PEDIDO"
Private Sub Class_Initialize()
Set dao = New DAOPedido
Set logDAO = New DAOLog
Set MesaDAO = New DAOMEsa
End Sub

Public Function enviar(bdetalle As bdetalle)
Set dao.Connection = utilConexion
dao.enviar bdetalle
End Function

Public Function getPedidoPorMesa(bean As bMesa) As bpedido
Dim result As Integer
Dim bpedido As bpedido
Set dao.Connection = utilConexion
Set bpedido = dao.getPedidoPorMesa(bean)
result = dao.getListaDetalle(bpedido)
Set getPedidoPorMesa = bpedido
If result = UtilConstantes.OPT_ERROR_ENCONTRADO Then
    capturarError UtilConstantes.STR_LISTAR
End If
End Function
Public Function getLista(bean As bpedido) As Collection
Set dao.Connection = utilConexion
Set getLista = dao.getLista(bean)
End Function
Public Function getListaPorRango(bean As BParmImpresionTickets) As Collection
Set dao.Connection = utilConexion
Set getListaPorRango = dao.getListaPorRango(bean)
End Function
Public Function getPagos(ByRef bean As bpedido) As Integer
Dim result As Integer
Set dao.Connection = utilConexion
result = dao.getPagos(bean)
If result = UtilConstantes.OPT_ERROR_ENCONTRADO Then
    capturarError UtilConstantes.STR_LISTAR
End If
End Function
Public Function guardar(bean As bpedido) As Integer
Set dao.Connection = utilConexion
If bean.ncodigo = 0 Then
    guardar = dao.guardarNuevo(bean)
Else
    guardar = dao.actualizar(bean)
End If
If guardar = UtilConstantes.OPT_ERROR_ENCONTRADO Then
    capturarError UtilConstantes.STR_ACTUALIZAR
End If
End Function
Public Function agregarDetalle(bean As bpedido) As Integer
    Set dao.Connection = utilConexion
    agregarDetalle = dao.guardaDetalle(bean)
    If agregarDetalle = UtilConstantes.OPT_DATOS_GUARDADOS Then
        calculaTotales bean
        agregarDetalle = dao.actualizar(bean)
    End If
    If agregarDetalle = UtilConstantes.OPT_ERROR_ENCONTRADO Then
    'capturarError UtilConstantes.STR_AGREGAR_DETALLE
End If
End Function
Private Function calculaTotales(ByRef bean As bpedido) As Integer
    bean.nTotal = bean.nTotalSinDscto - bean.nDescuento
    bean.nBasImponible = bean.nTotal / (1 + bparametro.nIgv + bparametro.nServicio)
    bean.nIgv = bean.nBasImponible * bparametro.nIgv
    bean.nServicio = bean.nBasImponible * bparametro.nServicio
End Function
Public Function eliminarDetalle(bdetalle As bdetalle, ByRef bean As bpedido) As Integer
    Set dao.Connection = utilConexion
    eliminarDetalle = dao.eliminarDetalle(bdetalle)
    dao.getListaDetalle bean
    calculaTotales bean
End Function
Public Function eliminarPedido(bean As bpedido) As Integer
    Set dao.Connection = utilConexion
    Set MesaDAO.Connection = utilConexion
    If bean.bEstado.ncodigo <> 0 And (bean.bEstado.ncodigo <> UtilConstantes.ESTADO_PEDIDO_PENDIENTE Or bean.bImpreso) Then
        eliminarPedido = UtilConstantes.OPT_PEDIDO_NO_ELIMINABLE
        Exit Function
    End If
    Dim bdetalle As New bdetalle
    
    MesaDAO.setLibre bean.bMesa
    eliminarPedido = dao.eliminar(bean)
End Function
Public Function getDetalle(ByRef bean As bpedido) As Integer
Set dao.Connection = utilConexion
dao.getListaDetalle bean
End Function
Public Function calcularPagos(ByRef bean As bpedido)
Dim i As Integer
Dim bPago As bPago
bean.nTotPagado = 0
bean.nCambio = 0
For i = 1 To bean.lPagos.Count
    Set bPago = bean.lPagos.Item(i)
    If bPago.BFormaPago.bEsDolar Then
        bean.nTotPagado = bean.nTotPagado + bPago.nMonto * bparametro.nTipoCambio
    Else
        bean.nTotPagado = bean.nTotPagado + bPago.nMonto
    End If
Next
If bean.nTotal < bean.nTotPagado Then
    bean.nCambio = bean.nTotPagado - bean.nTotal
Else
    bean.nCambio = 0
End If
End Function
Public Function aplicarDscto(ByRef bean As bpedido) As Integer
    If bean.bEstado.ncodigo <> UtilConstantes.ESTADO_PEDIDO_PENDIENTE Then
        aplicarDscto = UtilConstantes.OPT_DSCTO_NO_APLICADO
        Exit Function
    End If
    Set dao.Connection = utilConexion
    bean.nTotal = bean.nTotalSinDscto - bean.nDescuento
    aplicarDscto = dao.actualizar(bean)
End Function
Public Function guardarPagos(ByRef bean As bpedido) As Integer
If bean.nTotPagado >= bean.nTotal Then
    Set dao.Connection = utilConexion
    guardarPagos = dao.guardaPagos(bean)
Else
    guardarPagos = UtilConstantes.OPT_PEDIDO_FALTA_DESCARGAR
End If
End Function
Public Function cancelar(ByRef bean As bpedido, bImprimirTicket As Boolean) As Integer
If bean.nTotPagado >= bean.nTotal Then
    Set dao.Connection = utilConexion
    cancelar = dao.guardaPagos(bean)
    cancelar = dao.cancelar(bean)
    If cancelar = UtilConstantes.OPT_DATOS_GUARDADOS Then
        If bImprimirTicket Then
            cancelar = dao.asignarNumeracion(bean)
        End If
    Else
        Exit Function
    End If
    If cancelar = UtilConstantes.OPT_DATOS_GUARDADOS Then
        Set MesaDAO.Connection = utilConexion
        MesaDAO.setLibre bean.bMesa
    End If
Else
    cancelar = UtilConstantes.OPT_PEDIDO_FALTA_DESCARGAR
End If
End Function
Public Function anular(ByRef bean As bpedido) As Integer
Dim result As Integer
If bean.bEstado.ncodigo <> UtilConstantes.ESTADO_PEDIDO_CANCELADO Then
    result = UtilConstantes.OPT_PEDIDO_NO_ANULABLE
Else
    result = dao.anular(bean)
End If
anular = result
End Function
Public Function getListaDetalleImpresion(bpedido As bpedido) As Collection
Set dao.Connection = utilConexion
Set getListaDetalleImpresion = dao.getListaDetalleImpresion(bpedido)
End Function
Public Function asignarNumeracion(ByRef bean As bpedido) As Integer
    Set dao.Connection = utilConexion
    Set MesaDAO.Connection = utilConexion
    MesaDAO.setEnCaja bean.bMesa
    asignarNumeracion = dao.asignarNumeracion(bean)
End Function
Public Function actualizatdocumento(ByRef bean As bpedido) As Integer
    Set dao.Connection = utilConexion
    Set MesaDAO.Connection = utilConexion
    actualizatdocumento = dao.actualizatdocumento(bean)
End Function
Private Sub capturarError(opcion As String)
'Set bLog.busuario = bparametro.bUsuarioActivo
'bLog.sOpcion = opcion
'bLog.sModulo = MODULO
'If bConfiguracion.guardarHistorialSucesos Then
'    Set logDAO.Connection = utilConexion
'    logDAO.guardarNuevo bLog
'End If
If bConfiguracion.enviarMailSuceso Then
    'TO DO
End If
Set bLog = Nothing
End Sub

Public Function ActualizaEmpleadoDetalle(bean As bdetalle) As Integer
Set dao.Connection = utilConexion
    ActualizaEmpleadoDetalle = dao.ActualizaEmpleadoDetalle(bean)
If ActualizaEmpleadoDetalle = UtilConstantes.OPT_ERROR_ENCONTRADO Then
    capturarError UtilConstantes.STR_ACTUALIZAR
End If
End Function
